package com.sgb.zhaocai.viewmodel;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import androidx.lifecycle.ViewModel;
import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.DemandIssueBean;
import com.sgb.zhaocai.entity.FilterListEntity;
import com.sgb.zhaocai.entity.PublicTypeEntity;
import com.sgb.zhaocai.entity.PublishListBean;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.ArrayList;
import java.util.List;
import com.sgb.zhaocai.view.demand.activity.DemandIssueActivity;
import com.swgk.core.view.entity.MenuEntity;

/**
 * Time:2021/9/16
 * AUTHOR:F_Q
 * Description:
 */
public class DemandIssueModel extends ViewModel {

    /**
     * 申请单位列表
     */
    public void getCompanyList(DemandIssueActivity activity, View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMyCompanyList().enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setCompanyList(data.getData(), view);
                    } else {
                        MToast.showToast(activity, "当前用户没有其它公司信息");
                    }
                }
            }
        });
    }

    /**
     * 申请部门列表
     *
     * @param activity
     * @param applyCompany
     * @param view
     */
    public void getDepartmentList(DemandIssueActivity activity, String applyCompany, View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getDeptList(applyCompany).enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setDepartmentList(data.getData(), view);
                    } else {
                        MToast.showToast(activity, "当前单位无部门信息");
                    }
                }
            }
        });
    }

    /**
     * 用户列表信息
     *
     * @param activity
     * @param view
     * @param departmentId
     */
    public void getUserList(DemandIssueActivity activity, View view, String departmentId) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getPersonnel(departmentId).enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setUserList(data.getData(), view);
                    } else {
                        MToast.showToast(activity, data.getMsg());
                    }
                }
            }
        });
    }

    /**
     * 项目列表
     *
     * @param activity
     */
    public void getProjectNameList(DemandIssueActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getFilterList().enqueue(new ICallBack<BaseEntity<List<FilterListEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<FilterListEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setProjectNameList(data.getData());
                }
            }
        });
    }

    /**
     * 材料使用管理监控
     *
     * @param activity
     */
    public void getDemandTypeList(DemandIssueActivity activity, View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        List<PublishListBean> sources = new ArrayList<>();
        PublishListBean listBean3 = new PublishListBean();
        listBean3.setName("材料采购");
        listBean3.setId("3");
        sources.add(listBean3);
        PublishListBean listBean1 = new PublishListBean();
        listBean1.setName("机械租赁");
        listBean1.setId("1");
        sources.add(listBean1);
        PublishListBean listBean4 = new PublishListBean();
        listBean4.setName("设备租赁");
        listBean4.setId("5");
        sources.add(listBean4);
        PublishListBean listBean2 = new PublishListBean();
        listBean2.setName("劳务用工");
        listBean2.setId("2");
        sources.add(listBean2);
        DialogHelper.dismissProgressDialog();
        activity.setDemandTypeList(sources, view);
    }

    /**
     * 设备列表
     */
    @SuppressLint("CheckResult")
    public void getParentDeviceType(DemandIssueActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getDeviceType("0").enqueue(new ICallBack<BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setParentDeviceType(data.getData());
                    }
                }
            }
        });
    }

    /**
     * 机械租赁列表
     */
    public void getMachineTypeModel(DemandIssueActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMachineOutType().enqueue(new ICallBack<BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                        activity.setMachineTypeModel(data.getData());
                }
            }
        });
    }

    /**
     * 获取商品列表
     */
    public void getGoodsCategory(DemandIssueActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getGoodsCategory("").enqueue(new ICallBack<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                        activity.showGoodsTypePop(data.getData());
                }
            }
        });
    }

    /**
     * 需求管理-新增、编辑
     * @param activity
     * @param bean
     */
    public void getsaveOrUpdateBusinessPlan(DemandIssueActivity activity, DemandIssueBean bean) {
        Log.e("tag新增数据",bean.toString() );
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().saveOrUpdateBusinessPlan(bean).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setSaveOrUpdateBusinessPlan(bean.getAuditStatus());
                }else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 详情
     * @param activity
     * @param id
     */
    public void getWebH5(DemandIssueActivity activity, String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUrlSetting("Demandetails").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    String webUrl = data.getData().getUrl() + "?id=" + id;
                    activity.onSuccess(webUrl);
                }else {
                    MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }

    public void geDemandinfo(DemandIssueActivity activity, String id) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getBusinessPlanDetail(id).enqueue(new ICallBack<BaseEntity<DemandIssueBean>>() {
            @Override
            public void onSuccess(BaseEntity<DemandIssueBean> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData()!= null) {
                        activity.setDemandinfo(data.getData());
                    } else {
                        MToast.showToast(activity, data.getMsg());
                    }
                }
            }
        });
    }
}
